package com.gpframework.module.controller.module;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gpframework.common.annotation.Log;
import com.gpframework.common.core.controller.BaseController;
import com.gpframework.common.core.domain.AjaxResult;
import com.gpframework.common.core.page.TableDataInfo;
import com.gpframework.common.enums.BusinessType;
import com.gpframework.common.utils.poi.ExcelUtil;
import com.gpframework.module.manufacture.domain.req.GpProductionSchedulingUpdate;
import com.gpframework.module.manufacture.service.IGpProductionFlowService;
import com.gpframework.module.module.domain.GpDeliveryNote;
import com.gpframework.module.module.domain.req.GpDeliveryNoteAdd;
import com.gpframework.module.module.domain.req.GpDeliveryNoteReq;
import com.gpframework.module.module.domain.req.GpDeliveryNoteUpdate;
import com.gpframework.module.module.domain.vo.GpDeliveryNoteVo;
import com.gpframework.module.module.service.IGpDeliveryNoteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.List;
/**
 * @ClassName GpDeliveryNoteController
 * @Author gpframework
 * @Date 2023-12-11
 * @Version 1.0.0
 * @Description 到货单Controller
 */
@Validated
@Api(tags = "到货单")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@SuppressWarnings({"rawtypes","unchecked"})
@RequestMapping("/module/deliverynote" )
public class GpDeliveryNoteController extends BaseController {

    private final IGpDeliveryNoteService iGpDeliveryNoteService;
    private final IGpProductionFlowService iGpProductionFlowService;

 /**
  * @Description 分页
  * @Author gpframework
  * @Date 2023-12-11
  * @Param gpDeliveryNote
  * @Return TableDataInfo
 */
    @ApiOperation("列表")
    @PreAuthorize("@ss.hasPermi('module:deliverynote:list')")
    @GetMapping("/list")
    public TableDataInfo<GpDeliveryNoteVo> list(GpDeliveryNoteReq gpDeliveryNoteReq)
    {
        startPage();
        QueryWrapper<GpDeliveryNote> lqw = new QueryWrapper<GpDeliveryNote>();
        gpDeliveryNoteReq.generatorQuery(lqw,true);
        List<GpDeliveryNoteVo> list = iGpDeliveryNoteService.listVo(lqw);
        return getDataTable(list);
    }

    /**
     * @Description 新增
     * @Author gpframework
     * @Date 2023-12-11
     * @Param gpDeliveryNote
     * @Return TableDataInfo
     */
    @ApiOperation("新增")
    @PreAuthorize("@ss.hasPermi('module:deliverynote:add')" )
    @Log(title = "添加到货单" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Valid @RequestBody GpDeliveryNoteAdd gpDeliveryNoteAdd) {
        //判断订单号是否重复
        //if (StringUtils.isNotBlank(gpDeliveryNoteAdd.getSn())) {
        //    List<GpDeliveryNote> gpDeliveryNotes = iGpDeliveryNoteService.list(new QueryWrapper<GpDeliveryNote>());
        //    for (GpDeliveryNote gpDeliveryNote : gpDeliveryNotes) {
        //        if (gpDeliveryNoteAdd.getSn().equals(gpDeliveryNote.getSn())){
        //            throw new BaseException("订单号重复");
        //        }
        //    }
        //}
        GpDeliveryNote gpDeliveryNote = Convert.convert(new TypeReference<GpDeliveryNote>() {}, gpDeliveryNoteAdd);
        return toAjax(iGpDeliveryNoteService.save(gpDeliveryNote) ? 1 : 0);
    }

    /**
     * @Description 修改
     * @Author gpframework
     * @Date 2023-12-11
     * @Param gpDeliveryNote
     * @Return TableDataInfo
     */
    @ApiOperation("修改")
    @PreAuthorize("@ss.hasPermi('module:deliverynote:edit')" )
    @Log(title = "修改到货单" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Valid @RequestBody List<GpDeliveryNoteUpdate> list) {
        return toAjax(iGpDeliveryNoteService.edit(list) ? 1 : 0);
    }


    /**
     * @Description 导出
     * @Author gpframework
     * @Date 2023-12-11
     * @Param gpDeliveryNote
     * @Return TableDataInfo
     */

    @PreAuthorize("@ss.hasPermi('module:deliverynote:export')" )
    @Log(title = "导出到货单" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(GpDeliveryNoteReq gpDeliveryNoteReq) {
        QueryWrapper<GpDeliveryNote> lqw = new QueryWrapper<GpDeliveryNote>();
        gpDeliveryNoteReq.generatorQuery(lqw,true);
        List<GpDeliveryNoteVo> list = iGpDeliveryNoteService.listVo(lqw);
        ExcelUtil<GpDeliveryNoteVo> util = new ExcelUtil<GpDeliveryNoteVo>(GpDeliveryNoteVo. class);
        return util.exportExcel(list, "deliverynote" );
    }

    /**
     * @Description 详情
     * @Author gpframework
     * @Date 2023-12-11
     * @Param gpDeliveryNote
     * @Return TableDataInfo
     */
    @ApiOperation("详情")
    @PreAuthorize("@ss.hasPermi('module:deliverynote:query')" )
    @GetMapping(value = "/{id}" )
    public AjaxResult<GpDeliveryNoteVo> getInfo(@PathVariable("id" ) Long id) {
        return AjaxResult.success(iGpDeliveryNoteService.getVoById(id));
    }



    /**
     * @Description 删除
     * @Author gpframework
     * @Date 2023-12-11
     * @Param gpDeliveryNote
     * @Return TableDataInfo
     */
    @ApiOperation("删除")
    @PreAuthorize("@ss.hasPermi('module:deliverynote:remove')" )
    @Log(title = "删除到货单" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}" )
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(iGpDeliveryNoteService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }

    /**
     * @Description 生成来料检验单
     * @Author gpframework
     * @Date 2023-12-11
     * @Param gpDeliveryNote
     * @Return TableDataInfo
     */
    @ApiOperation("生成来料检验单")
    @PreAuthorize("@ss.hasPermi('module:deliverynote:add')" )
    @Log(title = "生成来料检验单" , businessType = BusinessType.INSERT)
    @PostMapping("/addIncomingInspect")
    public AjaxResult addIncomingInspect(@Valid @RequestBody GpDeliveryNoteUpdate gpDeliveryNoteUpdate) {
        if (gpDeliveryNoteUpdate == null || CollectionUtils.isEmpty(gpDeliveryNoteUpdate.getIds())) {
            return AjaxResult.parameterError();
        }
        return toAjax(iGpDeliveryNoteService.addIncomingInspect(gpDeliveryNoteUpdate) ? 1 : 0);
    }

    /**
     * @Description 生成生产跟踪单
     * @Author gpframework
     * @Date 2023-12-12
     * @Param gpProductionScheduling
     * @Return TableDataInfo
     */
    @ApiOperation("生成生产跟踪单")
    @PreAuthorize("@ss.hasPermi('module:deliverynote:add')" )
    @Log(title = "生成生产跟踪单" , businessType = BusinessType.INSERT)
    @PostMapping("/createProductionFlow")
    public AjaxResult createProductionFlow(@Valid @RequestBody GpProductionSchedulingUpdate gpProductionDetailsUpdate) {
        if (gpProductionDetailsUpdate == null || CollectionUtils.isEmpty(gpProductionDetailsUpdate.getIds())) {
            return AjaxResult.parameterError();
        }
        return toAjax(iGpProductionFlowService.createProductionFlow(gpProductionDetailsUpdate.getIds(), gpProductionDetailsUpdate.getCreateBy()) ? 1 : 0);
    }

    /**
     * @Description 复制
     * @Author gpframework
     * @Date 2023-12-11
     * @Param gpDeliveryNote
     * @Return TableDataInfo
     */
    @ApiOperation("复制")
    @PreAuthorize("@ss.hasPermi('module:deliverynote:edit')" )
    @Log(title = "复制" , businessType = BusinessType.UPDATE)
    @PostMapping("/copy")
    public AjaxResult copy(@Valid @RequestBody GpDeliveryNoteUpdate gpDeliveryNoteUpdate) {
        if (gpDeliveryNoteUpdate == null || gpDeliveryNoteUpdate.getId() == null || gpDeliveryNoteUpdate.getCopyNum() == null) {
            return AjaxResult.parameterError();
        }
        if (gpDeliveryNoteUpdate.getCopyNum() < 1) {
            return AjaxResult.error("复制条数不能小于1");
        }
        return toAjax(iGpDeliveryNoteService.copy(gpDeliveryNoteUpdate.getId(), gpDeliveryNoteUpdate.getCopyNum()) ? 1 : 0);
    }
}
